package com.newdjk.newdoctor.fragment;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.newdjk.newdoctor.MyApplication;
import com.newdjk.newdoctor.R;
import com.newdjk.newdoctor.base.BasicFragment;
import com.newdjk.newdoctor.net.BaseComPleteObserver;
import com.newdjk.newdoctor.net.NetServices;
import com.newdjk.newdoctor.ui.MessageActivity;
import com.newdjk.newdoctor.ui.OrderListActivity;
import com.newdjk.newdoctor.ui.SystemMessageActivity;
import com.newdjk.newdoctor.utils.AppLicationUtils;
import com.newdjk.newdoctor.utils.Contants;
import com.newdjk.newdoctor.utils.Event;
import com.newdjk.newdoctor.utils.GlideUtils;
import com.newdjk.newdoctor.utils.RxBus;
import com.newdjk.okhttp.entity.BaseEntity;
import com.newdjk.okhttp.entity.DoctorInfoEntity;
import com.newdjk.okhttp.entity.OrderListEntity;
import com.newdjk.okhttp.entity.UnreadMessageEntity;

import java.util.ArrayList;

import butterknife.BindView;
import io.reactivex.Observable;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.functions.Consumer;
import io.reactivex.schedulers.Schedulers;

public class MessageFragment extends BasicFragment {


    private static final String TAG = "MessageFragment";
    @BindView(R.id.system_unread_num)
    TextView systemUnreadNum;
    @BindView(R.id.system_unread_num_layout)
    LinearLayout systemUnreadNumLayout;
    @BindView(R.id.system_avatar_layout)
    RelativeLayout systemAvatarLayout;
    @BindView(R.id.system_content)
    TextView systemContent;
    @BindView(R.id.rv_system_message)
    RelativeLayout rvSystemMessage;
    @BindView(R.id.system_avatar)
    ImageView systemAvatar;
    @BindView(R.id.activity_unread_num)
    TextView activityUnreadNum;
    @BindView(R.id.activity_unread_num_layout)
    LinearLayout activityUnreadNumLayout;
    @BindView(R.id.activity_avatar_layout)
    RelativeLayout activityAvatarLayout;
    @BindView(R.id.activity_content)
    TextView activityContent;
    @BindView(R.id.rv_activity_message)
    RelativeLayout rvActivityMessage;
    @BindView(R.id.system_order)
    ImageView systemOrder;
    @BindView(R.id.order_unread_num)
    TextView orderUnreadNum;
    @BindView(R.id.order_unread_num_layout)
    LinearLayout orderUnreadNumLayout;
    @BindView(R.id.order_avatar_layout)
    RelativeLayout orderAvatarLayout;
    @BindView(R.id.order_content)
    TextView orderContent;
    @BindView(R.id.rv_order_message)
    RelativeLayout rvOrderMessage;
    private Observable<Boolean> orderlistObservable;
    private Observable<Boolean> systemObservable;
    private Observable<Boolean> loginObservable;
    private Observable<Boolean> activityObservable;

    @Override
    protected int initViewResId() {
        return R.layout.fragment_message;
    }

    @Override
    protected void initView() {

    }

    @SuppressLint("CheckResult")
    @Override
    protected void initListener() {
        rvSystemMessage.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(getContext(), SystemMessageActivity.class);
                intent.putExtra(Contants.TYPE, Contants.STSYEM);
                getActivity().startActivity(intent);
            }
        });

        rvActivityMessage.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(getContext(), MessageActivity.class);
                intent.putExtra(Contants.TYPE, Contants.ACTIVITY);
                getActivity().startActivity(intent);
            }
        });

        rvOrderMessage.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(getContext(), OrderListActivity.class);
                intent.putExtra(Contants.TYPE, Contants.ORDER);
                getActivity().startActivity(intent);
            }
        });

        orderlistObservable = RxBus.get().register(Event.jpush_ORDER_list);

        orderlistObservable.observeOn(AndroidSchedulers.mainThread()).subscribe(new Consumer<Boolean>() {
            @Override
            public void accept(Boolean aBoolean) throws Exception {
                Log.d(TAG, "MessageFragment更新订单列表");
                getOrderUnRednumber();
            }
        });

        systemObservable = RxBus.get().register(Event.jpush_Sysetm_list);

        systemObservable.observeOn(AndroidSchedulers.mainThread()).subscribe(new Consumer<Boolean>() {
            @Override
            public void accept(Boolean aBoolean) throws Exception {
                Log.d(TAG, "MessageFragment更新系统消息列表");
                getOrderUnRednumber();
            }
        });

        //更新活动数据
        activityObservable = RxBus.get().register(Event.jpush_activity_list);

        activityObservable.observeOn(AndroidSchedulers.mainThread()).subscribe(new Consumer<Boolean>() {
            @Override
            public void accept(Boolean aBoolean) throws Exception {
                Log.d(TAG, "MessageFragment活动消息列表");
                getOrderUnRednumber();
            }
        });
        loginObservable = RxBus.get().register(Event.Login_success);

        loginObservable.observeOn(AndroidSchedulers.mainThread()).subscribe(new Consumer<Boolean>() {
            @Override
            public void accept(Boolean aBoolean) throws Exception {
                getOrderUnRednumber();
            }
        });
    }

    @Override
    protected void initData() {

        getOrderUnRednumber();

    }

    private void getOrderUnRednumber() {

        if (MyApplication.isLogin) {
            if (MyApplication.LoginEntity != null) {
                NetServices.Factory.getService().OrgAppMessageGetUnReadNumByDrId(
                        MyApplication.LoginEntity.getUser().getDoctorId() + ""
                ).subscribeOn(Schedulers.io())
                        .observeOn(AndroidSchedulers.mainThread())
                        .subscribe(new BaseComPleteObserver<UnreadMessageEntity>(getContext(), false) {
                            @Override
                            protected void onSuccees(BaseEntity<UnreadMessageEntity> t) throws Exception {

                                try {
                                    Log.d(TAG, t.getData() + "");
                                    int allnumber = t.getData().getAllMessageNum();
                                    int activityRedNumnber = t.getData().getActivityMessageNum();
                                    int orderRedNumnber = t.getData().getOrderMessageNum();
                                    int systemRedNumnber = t.getData().getSysMessageNum();
                                    if (orderRedNumnber > 0) {
                                        orderUnreadNum.setText(orderRedNumnber + "");
                                        orderUnreadNumLayout.setVisibility(View.VISIBLE);
                                    } else {
                                        orderUnreadNumLayout.setVisibility(View.GONE);
                                    }

                                    if (activityRedNumnber > 0) {
                                        activityUnreadNum.setText(activityRedNumnber + "");
                                        activityUnreadNumLayout.setVisibility(View.VISIBLE);
                                    } else {
                                        activityUnreadNumLayout.setVisibility(View.GONE);
                                    }
                                    if (systemRedNumnber > 0) {
                                        systemUnreadNum.setText(systemRedNumnber + "");
                                        systemUnreadNumLayout.setVisibility(View.VISIBLE);
                                    } else {
                                        systemUnreadNumLayout.setVisibility(View.GONE);
                                    }
                                    RxBus.get().post(Event.Order_list_count, allnumber);

                                } catch (Exception e) {

                                }


                            }

                            @Override
                            protected void onFailure(Throwable e, String erroressage, boolean isNetWorkError) throws Exception {
                                super.onFailure(e, erroressage, isNetWorkError);

                            }
                        });
            }
        }
    }

    @Override
    protected void otherViewClick(View view) {

    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        RxBus.get().unregister(Event.jpush_ORDER_list, orderlistObservable);
        RxBus.get().unregister(Event.jpush_Sysetm_list, systemObservable);
        RxBus.get().unregister(Event.Login_success, loginObservable);
        RxBus.get().unregister(Event.jpush_activity_list, activityObservable);
    }
}
